package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IGatewayDao;
import com.manager.plat.dao.IPartnerUpperDao;
import com.manager.plat.vo.GatewayVo;
import com.manager.plat.vo.PartnerUpperVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class GatewayAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4991181033164937535L;

	private static Logger logger = Logger.getLogger(GatewayAction.class);
	
	private IGatewayDao gatewayDao;
	private IPartnerUpperDao partnerUpperDao;
	private List<GatewayVo> gatewayVoList;
	private List<PartnerUpperVo> partnerUpperVoList;
	private GatewayVo gatewayVo;
	private String actionPath;
	
	public String getGatewayList(){
		/**获得网关信息列表*/
		gatewayVoList = gatewayDao.getGatewayList();
		/**获得上游信息列表*/
		partnerUpperVoList = partnerUpperDao.getPartnerUpperList();
		return SUCCESS;
	}
	
	public String addGateway(){
		gatewayVo = null;
		/**获得上游信息列表*/
		partnerUpperVoList = partnerUpperDao.getPartnerUpperList();
		return SUCCESS;
	}
	
	public String editGateway(){
		if(gatewayVo.getId() != 0){
			gatewayVo = gatewayDao.getGatewayById(gatewayVo.getId());
		}
		
		/**获得上游信息列表*/
		partnerUpperVoList = partnerUpperDao.getPartnerUpperList();
		return SUCCESS;
	}
	
	public String updateGateway(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		/**判断是新增还是修改*/
		if(gatewayVo.getId() == 0){
			gatewayVo.setAdd_user(userInfoVo.getId());
			
			int count = gatewayDao.insertGateway(gatewayVo);
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]网关信息成功", userInfoVo.getLogin_username(),gatewayVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]添加[%s]网关信息失败", userInfoVo.getLogin_username(),gatewayVo.getSp_number()));
			}
			
		}else {
			gatewayVo.setUpdate_user(userInfoVo.getId());
			int count = gatewayDao.updateGateway(gatewayVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]网关信息成功", userInfoVo.getLogin_username(),gatewayVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]更新[%s]网关信息失败", userInfoVo.getLogin_username(),gatewayVo.getSp_number()));
			}
		}
		
		actionPath = "gateway/getGatewayList.action";
		return SUCCESS;
	}
	
	public String deleteGateway(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		int count = gatewayDao.deleteGateway(gatewayVo.getId(), userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]网关信息成功", userInfoVo.getLogin_username(),gatewayVo.getSp_number()));
		}else{
			logger.info(String.format("[%s]删除[%s]网关信息失败", userInfoVo.getLogin_username(),gatewayVo.getSp_number()));
		}
		actionPath = "gateway/getGatewayList.action";
		return SUCCESS;
	}
	
	public IGatewayDao getGatewayDao() {
		return gatewayDao;
	}
	public void setGatewayDao(IGatewayDao gatewayDao) {
		this.gatewayDao = gatewayDao;
	}
	public IPartnerUpperDao getPartnerUpperDao() {
		return partnerUpperDao;
	}
	public void setPartnerUpperDao(IPartnerUpperDao partnerUpperDao) {
		this.partnerUpperDao = partnerUpperDao;
	}
	public List<GatewayVo> getGatewayVoList() {
		return gatewayVoList;
	}
	public void setGatewayVoList(List<GatewayVo> gatewayVoList) {
		this.gatewayVoList = gatewayVoList;
	}
	public List<PartnerUpperVo> getPartnerUpperVoList() {
		return partnerUpperVoList;
	}
	public void setPartnerUpperVoList(List<PartnerUpperVo> partnerUpperVoList) {
		this.partnerUpperVoList = partnerUpperVoList;
	}
	public GatewayVo getGatewayVo() {
		return gatewayVo;
	}
	public void setGatewayVo(GatewayVo gatewayVo) {
		this.gatewayVo = gatewayVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
